package com.no1ikun.view;

import com.no1ikun.service.StudentService;
import com.no1ikun.service.StudentServiceImpl;
import com.no1ikun.util.MyButton;
import com.no1ikun.util.MyJLabel;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class IndexJPanel extends JPanel {
    private MyJTextField sousuo;
    private JTable table;
    private String[] title = {"学号","密码", "姓名", "性别", "生日", "籍贯", "生源地", "院系"};
    private MyButton ssbut;
    private StudentService ss = new StudentServiceImpl();
    public IndexJPanel() {
        //初始化数据
        init();
    }
    public void init() {
        this.setLayout(null);//绝对布局
        this.setBounds(80, 80, 885, 452);
        this.setBackground(Color.WHITE);
        MyJLabel time = new MyJLabel("请输入要搜索的内容", 200, 30, 60, 25, "微软雅黑", Font.BOLD, 17, new Color(28, 136, 201));
        sousuo = new MyJTextField(220, 27, 210, 25);
        sousuo.setBorder(new LineBorder(new Color(28, 136, 201)));
        //添加搜索按钮
        ssbut = new MyButton("搜索", 440, 27, 60, 25, new Color(28, 136, 201), Color.WHITE);
        //去数据库查询出数据
        Object[][] data = ss.getData();
        //要一个二维数组
        DefaultTableModel defatleTable = new DefaultTableModel(data, title){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        //定义一个表格
        table = new JTable(defatleTable);
        //设置行高
        table.setRowHeight(25);

        //创建一个渲染器
        DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
        //内容居中显示
        dc.setHorizontalAlignment(JLabel.CENTER);

        table.setDefaultRenderer(Object.class, dc);
        //设置指定类的宽
        table.getColumnModel().getColumn(5).setPreferredWidth(100);
        table.getColumnModel().getColumn(6).setPreferredWidth(120);
        //添加一个滚动条
        JScrollPane js = new JScrollPane(table);
        js.setBounds(10, 70, 865, 375);


        this.add(ssbut);
        this.add(sousuo);
        this.add(time);
        this.add(js);

        //监听搜索按钮的监听事件
        ssbut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //1.接收用户要检索的数据
                String text = sousuo.getText();
                System.out.println("要检索的数据是:" + text);

                if (!"".equals(text)) {
                    //去数据库查询
                    Object[][] stus = ss.selectLikeStudent(text);

                    //重新完成数据渲染
                    TableModel dataModel = new DefaultTableModel(stus, title);
                    table.setModel(dataModel);

                }else{
                    Object[][] data1 = ss.getData();
                    //重新完成数据渲染
                    TableModel dataModel = new DefaultTableModel(data1, title);
                    table.setModel(dataModel);
                }

            }
        });

    }
}
